var SPLIT1 = "split";
function readExtends(obj,bn){
	if(obj.address){
		var address = obj.address;
		$("#address1").val(address.substring(0,address.indexOf('省')));
		$("#address2").val(address.substring(address.indexOf('省')+1,address.indexOf('市')));
		$("#address3").val(address.substring(address.indexOf('镇')+1,address.indexOf('区')));
		$("#address4").val(address.substring(address.indexOf('县')+1));
	}
}
function saveExtends(bn){
	//处理现居住址
	var address = $("#address1").val()+'省'+$("#address2").val()+'市/镇'+$("#address3").val()+'区/县'+$("#address4").val();
	bn.obj.address = address;
}

//不使用优惠券
$(".no-coupon").click(function () {
    $("#dnaMotherIdCard").removeAttr('disabled');
    $("#sampleName").removeAttr('disabled');
    $('#myModal').modal('hide');
    couponNum = '';
    $('.coupon-box').html('选择免费券');
});

//退出登录
function logout() {
    $.ajax({
        url: '/csfy/dologout.do',
        method: 'POST',
        success: function (res) {
            if (JSON.parse(res).code == 200) {
                window.location.href = "/csfy/index.do"
            }
        }
    })
}
//使用优惠券
var couponNum = '';

//受检者姓名
var sampleName = '';

function useCoupon(num) {
    //优惠券对应的身份证号码
    var id = $("#coupon-kw").val();
    //设置优惠券对应的身份证
    $("#idCard").val(id);
    //设置身份证输入框不可编辑
    $("#idCard").attr('disabled', 'disabled');

    //使用优惠券姓名
    $("#sampleName").val(sampleName);
    $("#sampleName").attr('disabled', 'disabled');

    $('#myModal').modal('hide');
    $('.coupon-box').html('免费券码：' + (num ? num : ''));

    //重置搜索列表
    $("#coupon-kw").val('');
    $(".coupon-list").html('');
    $(".empty-coupon").hide();

    couponNum = num;
}
//TODO 根据证件搜索优惠券
function searchCoupons() {
    var id = $("#coupon-kw").val();
    if (id) {
        $.ajax({
            method: 'POST',
            url: '/csfy/intf.do',
            data: {
                method: 'getDetailPresonInfo',
                id
            },
            success: function (res) {
                console.log(res)
                var couponList = '';
                sampleName = res.rows.name;
                $(".coupon-list").html('');
                $(".empty-coupon").html('正在搜索...');
                if (res && res.code == 200) {
                    var couponInfos = (res.rows && res.rows.couponInfos) || [];
                    if (couponInfos.length > 0) {
                        var mData = {};
                        mData.rows = couponInfos;
                        var html = template('coupon-list', mData);
                        $(".coupon-list").html(html);
                        $(".empty-coupon").hide();
                    } else {
                        $(".empty-coupon").html('未找到当前证件名下的免费券');
                    }
                } else {
                    $(".empty-coupon").html('未找到当前证件名下的免费券');
                }
            }
        });
    } else {
        $(".coupon-list").html('');
        $(".empty-coupon").show();
        $(".empty-coupon").html('没有找到免费券');
    }
}

//选择使用优惠券
function chooseCoupon() {
    $('#myModal').modal('show');
}

function save(cover) {
    //获取当前页面的子页面里面的属性值
    var msg = temp.validate();
    //获取模板字段。
    var obj = temp.saveAbs();
    //报错并退出
    if (msg) {
        sysErrorShow(msg);
        return false;
    }

    //数据过滤 start
    var params = {};
    Object.keys(obj).map(key => {
        var prototype = Object.prototype.toString.call(obj[key]);
    if ('[object String]' === prototype && obj[key]) {
        params[key] = obj[key];
    }
});
    params.productName = 'HPV检测';
    params.productCode = 'DX0532';
    params.couponNum = undefined;
    if (couponNum) {
        params.couponNum = couponNum;
    }

    //设置模板文件路径，对应前端工作平台1.0的模板服务器路径
    params.fileVer = "temp/HPV.jsp";

    //数据过滤 end

    $.ajax({
            type: 'POST',
            data: {
                method: isEdit ? 'updateSampleBaseBySampleBaseId' : 'createSampleBase',
                ...params,
            sampleBaseId: isEdit ? sampleBaseId : undefined
        },
        url: '/csfy/intf.do',
        error: function (e) {
        sysErrorShow('操作失败!');
    },
    success: function (res) {
        if (res && res.code == 200) {
            alert(isEdit ? "修改送检单成功" : "保存送检单成功");
            if (!isEdit)
                window.location.href = window.location.pathname;
            else
                sessionStorage.setItem("sampleEditSuccess", "true");
        } else if (res) {
            alert(res.msg ? res.msg : "操作失败")
        }
    }
});
}
//获取当前登录用户信息
$(document).ready(function () {
    $.ajax({
        method: 'POST',
        url: '/csfy/intf.do',
        data: { method: 'getLoginUserInfo' },
        success: res => {
        if (res.code == 10086) {
        alert("请重新登录！");
    } else {
        if (res.code == 200) {
            $(".username").html(res.rows.userName);
        }
    }
}
})
})